/*
 * TransactionLineBO.java
 *
 * Created on March 5, 2007, 4:19 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.jjenkins.data;

 /**
 * The class is represents the transactionLine business object. It is created
 * by the transactionLineDAO. It extends businessObject which contains the ID,
 * alreadyInDatabase and dirty. This class contains the attributes of the transaction
 * lines with all getters and setters and any functionality of the object. 
 * 
 * @author      Jeff Jenkins
 * @version     1.0
 * 
 */
public class TransactionLineBO extends BusinessObject{
    
    private String revenueSource = null;
    private String coupon = null;
    private String transaction = null; // I choose to make this just the id, because normally when we want to read a transaction_line alone without first reading the transaction, they would probably not need the transactionBO with it. This is done to save space. If it is need it is read in the get method
    private double subtotal = 0;
    private boolean dirtySubtotal = false;
    private String status = "normal";
    
    /**
     * translineBO
     */
    public TransactionLineBO() {
    }
    
    /**
     * sets teh coupon id
     * @param coupon 
     */
    public void setCouponID(String coupon){
        this.coupon = coupon;
    }
    
    /**
     * sets the revenue source id
     * @param revenueSource 
     */
    public void setRevenueSourceID(String revenueSource){
        this.revenueSource = revenueSource;
    }
    /*This method returns the revenueSource
     *@return   RevenueSourceBO
     *@throws: dataException if no revenue source exists*/
    
    /**
     * 
     * gets the revneue source
     * @return 
     * @throws edu.byu.isys413.jjenkins.data.DataException 
     */
    public RevenueSourceBO getRevenueSource() throws DataException{
        if(revenueSource == null){
            throw new DataException("You must enter a revenue source of a transaction line");
        }else{
            // This gets the revenue source type then it gets teh revenue source dao then it reads
           String rstype = RevenueSourceDAO.getType(revenueSource);
           System.out.println(rstype);
            return (RevenueSourceFactory.getInstance().getDAO(rstype)).read(revenueSource);
        }
    }
    
      /**
     * This method is sort of a hash. When an or save is called on Transaction Line,
     * the calling method does not want the whole object, it just wants the id. So
     * this method just returns the id. Also I couln't find a way to have it get the object
     * anyways without requiring additional connections
     * @return revenue source
     */
    
    public String getRevenueSourceID() {
        
        return revenueSource;
    }
    
    /**
     * This method sets the revenueSource
     * @par RevenueSourceBO
     * throws DataException
     * @param revenueSource 
     * @throws edu.byu.isys413.jjenkins.data.DataException 
     */
    
    public void setRevenueSource(RevenueSourceBO revenueSource) throws DataException{
        this.revenueSource = revenueSource.getId();
        setDirty(true);
    }
    
    /**
     * This method returns the coupon
     * @return CouponBO
     * @throws edu.byu.isys413.jjenkins.data.DataException 
     */
    
    public ConceptualCouponBO getCoupon() throws DataException{
        
        // ToDo: You should also check to see if the coupon applies to the
        // correct revenue source at this point.
        
        if(coupon !=null){
            return ConceptualCouponDAO.getInstance().read(coupon);
            
        }else{
            return null;
        }
    }
    
    
      /**
     * This method is sort of a hash. When an or save is called on Transaction Line,
     * the calling method does not want the whole object, it just wants the id. So
     * this method just returns the id. Also I couln't find a way to have it get the object
     * anyways without requiring additional connections
     * @return coupon 
     */
    
    public String getCouponID() {
        
        return coupon;
    }
     /**
     * This method sets the coupon id.
     * 
     * @par CouponBO
     * @param coupon 
     * @throws edu.byu.isys413.jjenkins.data.DataException 
     */
    
    public void setCoupon(ConceptualCouponBO coupon) throws DataException{
        this.coupon = coupon.getId();
        setDirty(true);
    }
    
    
  /*This method returns the Transaction
   *@return   TransactionBO*/
    /**
     * gets the transaction
     * @return 
     * @throws edu.byu.isys413.jjenkins.data.DataException 
     */
    public TransactionBO getTransaction() throws DataException{
        
        
        return TransactionDAO.getInstance().read(transaction);
    }
    
  /**
     * This method is sort of a hash. When an or save is called on Transaction Line,
     * the calling method does not want the whole object, it just wants the id. So
     * this method just returns the id. Also I couln't find a way to have it get the object
     * anyways without requiring additional connections
     * @return transaction id
     */
    
    public String getTransactionID() {
        
        return transaction;
    }
    
  /**
     * This method sets the Transaction
     * @par TransactionBO
     * @param transaction 
     */
    public void setTransaction(TransactionBO transaction) {
        this.transaction = transaction.getId();
        setDirty(true);
    }
    
    /**
     * sets the transaction id
     * @param id 
     */
    public void setTransactionID(String id){
        this.transaction = id;
    }
    
    
  /**
     * This method calculates and returns the subTotal
     * @return double
     * @throws edu.byu.isys413.jjenkins.data.DataException 
     */
    public double getSubtotal()throws DataException{
        
        RevenueSourceBO rev = getRevenueSource();
        
        ConceptualCouponBO c = getCoupon();
        double amount;
        
        // If there is a coupon it wil get the amount
        if(coupon != null){
            amount = c.getAmount();
            
        }else{
            amount = 0;
        }

        setSubtotal(rev.getPrice()*rev.getQuantity() - amount);
        return subtotal;
    }
    
  /*This method sets the subTotal
   *@par   SubtotalBO*/
    /**
     * sets the subtotal
     * @param subtotal 
     */
    public void setSubtotal(double subtotal) {
        this.subtotal = subtotal;
        setDirty(true);
    }
    /**this gets the subtotal without having to calculat it all over again
     *i use this for returns
     *
     *@return subtotal
     */
    public double getRSubtotal(){
        return subtotal;
    }

    /**
     * gets the status of the trasnactionline
     * @return status
     */
    public String getStatus() {
        return status;
    }

    /**
     * sets the status of the transaction line
     * @param status 
     */
    public void setStatus(String status) {
        this.status = status;
        setDirty(true);
    }
    
    
    
    
}
